import Koa from 'koa'
import bodyParser from 'koa-bodyparser'
import router from './routes'
import errorHandler from './middlewares/error'
import allowOriginHandler from './middlewares/allow-origin'
import { tokenHandler } from './middlewares/token'
import jwt from 'koa-jwt'
import { secret_key } from './config/secret'

const app = new Koa()

// 中间件加载
app.use(tokenHandler)
app
  .use(bodyParser())
  .use(jwt({ secret: secret_key, cookie: 'token' }).unless({ path: [/\/login/, /\/tableConfig\/getCode/] }))
app.use(errorHandler).use(allowOriginHandler)
app.use(router.routes()).use(router.allowedMethods())

const PORT = process.env.PORT || 3000
app.listen(PORT, () => {
  console.log(`Server running on http://localhost:${PORT}`)
})
